#include <stdio.h>
#include <math.h>
#include <stdbool.h>

/* 定义结构体存储不同类型热电偶的参数 */
typedef struct
{
    float c[15];
    float a[4];
    int order;
} CoifficientsConfig;
/**
 * @brief 检查给定字符是否在指定的字符集中
 *
 * 该函数会遍历预定义的字符集，判断传入的字符是否存在于该字符集中。
 *
 * @param ch 待检查的字符
 * @return bool 如果字符存在于字符集中返回 true，否则返回 false
 */
bool is_incharset(char ch)
{
    // 定义指定的字符集
    char charSet[] = {'k', 'b', 'e', 'j', 'n', 't', 'r', 's', 'q'};
    int setSize = sizeof(charSet) / sizeof(charSet[0]);
    // 检查字符是否在字符集中
    for (int i = 0; i < setSize; i++)
    {
        if (ch == charSet[i])
        {
            return true;
        }
    }
    return false;
}
float get_min_temp(char type)
{
    float res = 0.0;
    switch (type)
    {
    case 'k':
        res = -270.0; // K 型热电偶的最小温度
        break;
    case 'b':
        res = 0.0; // B 型热电偶的最小温度
        break;
    case 'e':
        res = -270.0; // E 型热电偶的最小温度
        break;
    case 'j':
        res = -210.0; // J 型热电偶的最小温度
        break;
    case 'n':
        res = -270.0; // N 型热电偶的最小温度
        break;
    case 't':
        res = -270.0; // T 型热电偶的最小温度
        break;
    case 'r':
        res = -50.0; // R 型热电偶的最小温度
        break;
    case 's':
        res = -50.0; // S 型热电偶的最小温度
        break;
    default:
        break;
    }
    return res;
}
float get_max_temp(char type)
{
    float res = 0.0;
    switch (type)
    {
    case 'k':
        res = 1372.0; // K 型热电偶的最大温度
        break;
    case 'b':
        res = 1820.0; // B 型热电偶的最大温度
        break;
    case 'e':
        res = 1000.0; // E 型热电偶的最大温度
        break;
    case 'j':
        res = 1200.0; // J 型热电偶的最大温度
        break;
    case 'n':
        res = 1300.0; // N 型热电偶的最大温度
        break;
    case 't':
        res = 400.0; // T 型热电偶的最大温度
        break;
    case 'r':
        res = 1768.0; // R 型热电偶的最大温度
        break;
    case 's':
        res = 1768.0; // S 型热电偶的最大温度
        break;
    default:
        break;
    }
    return res;
}
// 强制内联的高性能计算函数
static inline float compute_formula(float a0, float a1, float a2, float t0)
{
    float diff = t0 - a2;          // 计算 (t0 - a2)
    float squared = diff * diff;   // 计算 (t0 - a2)^2
    return a0 * exp(a1 * squared); // 计算 a0 * e^{a1 * squared}
}

// 高性能多项式计算（霍纳法则）
float etaluate_polynomial(const float *c, int n, float t)
{
    float result = c[n]; // 初始化最高次项系数
    for (int i = n - 1; i >= 0; i--)
    {
        result = result * t + c[i]; // 嵌套乘法
    }
    return result;
}
/**
 *  计算 不同类型 热电偶的热电势值
 *
 * @param config 不同类型热电偶系数
 * @param type 热电偶类型，k/K,b/B,e/E,j/J,n/N,t/T,r/R,s/S
 * @param t 温度 摄氏度
 * @return float 热电势值 mt
 *
 * */
float calculateE(CoifficientsConfig *config, char type, float t)
{
    float sum = 0.0;
    // float x = 1.0;
    // for (int i = 0; i <= config->order; i++)
    // {
    //     sum += config->c[i] * x;
    //     x *= t;
    // }
    // if (t > 0)
    // {
    //     sum += compute_formula(config->a[0], config->a[1], config->a[2], t);
    // }
    sum += etaluate_polynomial(config->c, config->order, t);
    if (type == 'k' || type == 'K' && t > 0)
    {
        sum += compute_formula(config->a[0], config->a[1], config->a[2], t);
    }
    return sum;
}
/* K 型热电偶 */
float calculate_k(float t)
{
    float res = 0.0;
    if (t >= -270.00 && t < 0.00)
    {
        CoifficientsConfig k_config = {
            .c = {0},
            .a = {0},
            .order = 10};
        k_config.c[0] = 0.000000000000E+00;
        k_config.c[1] = 0.394501280250E-01;
        k_config.c[2] = 0.236223735980E-04;
        k_config.c[3] = -0.328589067840E-06;
        k_config.c[4] = -0.499048287770E-08;
        k_config.c[5] = -0.675090591730E-10;
        k_config.c[6] = -0.574103274280E-12;
        k_config.c[7] = -0.310888728940E-14;
        k_config.c[8] = -0.104516093650E-16;
        k_config.c[9] = -0.198892668780E-19;
        k_config.c[10] = -0.163226974860E-22;
        res = calculateE(&k_config, 'k', t);
    }
    if (t >= 0.00 && t <= 1372.00)
    {
        CoifficientsConfig k_config = {
            .c = {0},
            .a = {0.118597600000, -0.118343200000E-03, 0.126968600000E+03},
            .order = 9};
        k_config.c[0] = -0.176004136860E-01;
        k_config.c[1] = 0.389212049750E-01;
        k_config.c[2] = 0.185587700320E-04;
        k_config.c[3] = -0.994575928740E-07;
        k_config.c[4] = 0.318409457190E-09;
        k_config.c[5] = -0.560728448890E-12;
        k_config.c[6] = 0.560750590590E-15;
        k_config.c[7] = -0.320207200030E-18;
        k_config.c[8] = 0.971511471520E-22;
        k_config.c[9] = -0.121047212750E-25;
        res = calculateE(&k_config, 'k', t);
    }
    return res;
}
/* B 型热电偶 */
float calculate_b(float t)
{
    float res = 0.0;
    if (t >= 0.00 && t < 630.615)
    {
        CoifficientsConfig b_config = {
            .c = {0},
            .a = {0},
            .order = 6};
        b_config.c[0] = 0.000000000000E+00;
        b_config.c[1] = -0.246508183460E-03;
        b_config.c[2] = 0.590404211710E-05;
        b_config.c[3] = -0.132579316360E-08;
        b_config.c[4] = 0.156682919010E-11;
        b_config.c[5] = -0.169445292400E-14;
        b_config.c[6] = 0.629903470940E-18;
        res = calculateE(&b_config, 'b', t);
    }
    if (t >= 630.615 && t <= 1820.00)
    {
        CoifficientsConfig b_config = {
            .c = {0},
            .a = {0},
            .order = 8};
        b_config.c[0] = -0.389381686210E+01;
        b_config.c[1] = 0.285717474700E-01;
        b_config.c[2] = -0.848851047850E-04;
        b_config.c[3] = 0.157852801640E-06;
        b_config.c[4] = -0.168353448640E-09;
        b_config.c[5] = 0.111097940130E-12;
        b_config.c[6] = -0.445154310330E-16;
        b_config.c[7] = 0.989756408210E-20;
        b_config.c[8] = -0.937913302890E-24;
        res = calculateE(&b_config, 'b', t);
    }

    return res;
}
/* E 型热电偶 */
float calculate_e(float t)
{
    float res = 0.0;
    if (t >= -270 && t < 0.00)
    {
        CoifficientsConfig e_config = {
            .c = {0},
            .a = {0},
            .order = 13};
        e_config.c[0] = 0.000000000000E+00;
        e_config.c[1] = 0.586655087080E-01;
        e_config.c[2] = 0.454109771240E-04;
        e_config.c[3] = -0.779980486860E-06;
        e_config.c[4] = -0.258001608430E-07;
        e_config.c[5] = -0.594525830570E-09;
        e_config.c[6] = -0.932140586670E-11;
        e_config.c[7] = -0.102876055340E-12;
        e_config.c[8] = -0.803701236210E-15;
        e_config.c[9] = -0.439794973910E-17;
        e_config.c[10] = -0.164147763550E-19;
        e_config.c[11] = -0.396736195160E-22;
        e_config.c[12] = -0.558273287210E-25;
        e_config.c[13] = -0.346578420130E-28;
        res = calculateE(&e_config, 'e', t);
    }
    if (t >= 0.00 && t <= 1000.00)
    {
        CoifficientsConfig e_config = {
            .c = {0},
            .a = {0},
            .order = 10};
        e_config.c[0] = 0.000000000000E+00;
        e_config.c[1] = 0.586655087100E-01;
        e_config.c[2] = 0.450322755820E-04;
        e_config.c[3] = 0.289084072120E-07;
        e_config.c[4] = -0.330568966520E-09;
        e_config.c[5] = 0.650244032700E-12;
        e_config.c[6] = -0.191974955040E-15;
        e_config.c[7] = -0.125366004970E-17;
        e_config.c[8] = 0.214892175690E-20;
        e_config.c[9] = -0.143880417820E-23;
        e_config.c[10] = 0.359608994810E-27;
        res = calculateE(&e_config, 'e', t);
    }
    return res;
}
/* J 型热电偶 */
float calculate_j(float t)
{
    float res = 0.0;
    if (t >= -210.00 && t < 760.00)
    {
        CoifficientsConfig j_config = {
            .c = {0},
            .a = {0},
            .order = 8};
        j_config.c[0] = 0.000000000000E+00;
        j_config.c[1] = 0.503811878150E-01;
        j_config.c[2] = 0.304758369300E-04;
        j_config.c[3] = -0.856810657200E-07;
        j_config.c[4] = 0.132281952950E-09;
        j_config.c[5] = -0.170529583370E-12;
        j_config.c[6] = 0.209480906970E-15;
        j_config.c[7] = -0.125383953360E-18;
        j_config.c[8] = 0.156317256970E-22;
        res = calculateE(&j_config, 'j', t);
    }
    if (t >= 760.00 && t <= 1200.00)
    {
        CoifficientsConfig j_config = {
            .c = {0},
            .a = {0},
            .order = 5};
        j_config.c[0] = 0.296456256810E+03;
        j_config.c[1] = -0.149761277860E+01;
        j_config.c[2] = 0.317871039240E-02;
        j_config.c[3] = -0.318476867010E-05;
        j_config.c[4] = 0.157208190040E-08;
        j_config.c[5] = -0.306913690560E-12;
        res = calculateE(&j_config, 'j', t);
    }
    return res;
}
/* N 型热电偶 */
float calculate_n(float t)
{
    float res = 0.0;
    if (t >= -270.00 && t < 0.00)
    {
        CoifficientsConfig n_config = {
            .c = {0},
            .a = {0},
            .order = 8};
        n_config.c[0] = 0.000000000000E+00;
        n_config.c[1] = 0.261591059620E-01;
        n_config.c[2] = 0.109574842280E-04;
        n_config.c[3] = -0.938411115540E-07;
        n_config.c[4] = -0.464120397590E-10;
        n_config.c[5] = -0.263033577160E-11;
        n_config.c[6] = -0.226534380030E-13;
        n_config.c[7] = -0.760893007910E-16;
        n_config.c[8] = -0.934196678350E-19;
        res = calculateE(&n_config, 'n', t);
    }
    if (t >= 0.00 && t <= 1300)
    {
        CoifficientsConfig n_config = {
            .c = {0},
            .a = {0},
            .order = 10};
        n_config.c[0] = 0.000000000000E+00;
        n_config.c[1] = 0.259293946010E-01;
        n_config.c[2] = 0.157101418800E-04;
        n_config.c[3] = 0.438256272370E-07;
        n_config.c[4] = -0.252611697940E-09;
        n_config.c[5] = 0.643118193390E-12;
        n_config.c[6] = -0.100634715190E-14;
        n_config.c[7] = 0.997453389920E-18;
        n_config.c[8] = -0.608632456070E-21;
        n_config.c[9] = 0.208492293390E-24;
        n_config.c[10] = -0.306821961510E-28;
        res = calculateE(&n_config, 'n', t);
    }
    return res;
}
/* T 型热电偶 */
float calculate_t(float t)
{
    float res = 0.0;
    if (t >= -270.00 && t < 0.00)
    {
        CoifficientsConfig t_config = {
            .c = {0},
            .a = {0},
            .order = 14};
        t_config.c[0] = 0.000000000000E+00;
        t_config.c[1] = 0.387481063640E-01;
        t_config.c[2] = 0.441944343470E-04;
        t_config.c[3] = 0.118443231050E-06;
        t_config.c[4] = 0.200329735540E-07;
        t_config.c[5] = 0.901380195590E-09;
        t_config.c[6] = 0.226511565930E-10;
        t_config.c[7] = 0.360711542050E-12;
        t_config.c[8] = 0.384939398830E-14;
        t_config.c[9] = 0.282135219250E-16;
        t_config.c[10] = 0.142515947790E-18;
        t_config.c[11] = 0.487686622860E-21;
        t_config.c[12] = 0.107955392700E-23;
        t_config.c[13] = 0.139450270620E-26;
        t_config.c[14] = 0.797951539270E-30;
        res = calculateE(&t_config, 't', t);
    }
    if (t >= 0.00 && t <= 400)
    {
        CoifficientsConfig t_config = {
            .c = {0},
            .a = {0},
            .order = 8};
        t_config.c[0] = 0.000000000000E+00;
        t_config.c[1] = 0.387481063640E-01;
        t_config.c[2] = 0.332922278800E-04;
        t_config.c[3] = 0.206182434040E-06;
        t_config.c[4] = -0.218822568460E-08;
        t_config.c[5] = 0.109968809280E-10;
        t_config.c[6] = -0.308157587720E-13;
        t_config.c[7] = 0.454791352900E-16;
        t_config.c[8] = -0.275129016730E-19;
        res = calculateE(&t_config, 't', t);
    }
    return res;
}
/* R 型热电偶 */
float calculate_r(float t)
{
    float res = 0.0;
    if (t >= -50.00 && t < 1064.18)
    {
        CoifficientsConfig r_config = {
            .c = {0},
            .a = {0},
            .order = 9};
        r_config.c[0] = 0.000000000000E+00;
        r_config.c[1] = 0.528961729765E-02;
        r_config.c[2] = 0.139166589782E-04;
        r_config.c[3] = -0.238855693017E-07;
        r_config.c[4] = 0.356916001063E-10;
        r_config.c[5] = -0.462347666298E-13;
        r_config.c[6] = 0.500777441034E-16;
        r_config.c[7] = -0.373105886191E-19;
        r_config.c[8] = 0.157716482367E-22;
        r_config.c[9] = -0.281038625251E-26;
        res = calculateE(&r_config, 'r', t);
    }
    if (t >= 1064.18 && t < 1664.50)
    {
        CoifficientsConfig r_config = {
            .c = {0},
            .a = {0},
            .order = 5};
        r_config.c[0] = 0.295157925316E+01;
        r_config.c[1] = -0.252061251332E-02;
        r_config.c[2] = 0.159564501865E-04;
        r_config.c[3] = -0.764085947576E-08;
        r_config.c[4] = 0.205305291024E-11;
        r_config.c[5] = -0.293359668173E-15;
        res = calculateE(&r_config, 'r', t);
    }
    if (t >= 1664.50 && t <= 1768.10)
    {
        CoifficientsConfig r_config = {
            .c = {0},
            .a = {0},
            .order = 4};
        r_config.c[0] = 0.152232118209E+03;
        r_config.c[1] = -0.268819888545E+00;
        r_config.c[2] = 0.171280280471E-03;
        r_config.c[3] = -0.345895706453E-07;
        r_config.c[4] = -0.934633971046E-14;
        res = calculateE(&r_config, 'r', t);
    }
    return res;
}
/* S 型热电偶 */
float calculate_s(float t)
{
    float res = 0.0;
    if (t >= -50.00 && t < 1064.18)
    {
        CoifficientsConfig s_config = {
            .c = {0},
            .a = {0},
            .order = 8};
        s_config.c[0] = 0.000000000000E+00;
        s_config.c[1] = 0.540313308631E-02;
        s_config.c[2] = 0.125934289740E-04;
        s_config.c[3] = -0.232477968689E-07;
        s_config.c[4] = 0.322028823036E-10;
        s_config.c[5] = -0.331465196389E-13;
        s_config.c[6] = 0.255744251786E-16;
        s_config.c[7] = -0.125068871393E-19;
        s_config.c[8] = 0.271443176145E-23;
        res = calculateE(&s_config, 's', t);
    }
    if (t >= 1064.18 && t < 1664.50)
    {
        CoifficientsConfig s_config = {
            .c = {0},
            .a = {0},
            .order = 4};
        s_config.c[0] = 0.132900444085E+01;
        s_config.c[1] = 0.334509311344E-02;
        s_config.c[2] = 0.654805192818E-05;
        s_config.c[3] = -0.164856259209E-08;
        s_config.c[4] = 0.129989605174E-13;
        res = calculateE(&s_config, 's', t);
    }
    if (t >= 1664.50 && t <= 1768.10)
    {
        CoifficientsConfig s_config = {
            .c = {0},
            .a = {0},
            .order = 4};
        s_config.c[0] = 0.146628232636E+03;
        s_config.c[1] = -0.258430516752E+00;
        s_config.c[2] = 0.163693574641E-03;
        s_config.c[3] = -0.330439046987E-07;
        s_config.c[4] = -0.943223690612E-14;
        res = calculateE(&s_config, 's', t);
    }
    return res;
}

int main()
{
    char type;
    float t = 0.0;
    int running = 1; // 用于控制循环的标志
    float res = 0.0;
    while (running)
    {
        printf("请输入热电偶类型(`q`退出): k/b/e/j/n/t/r/s或K/B/E/J/N/T/R/S\n");
        type = getchar();
        if (type >= 'A' && type <= 'Z')
            type += 32; // 转换为小写字母
        while (!is_incharset(type))
        {
            printf("输入错误，请重新输入\n");
            while (getchar() != '\n')
                ; // 清空输入缓冲区中的错误字符
            type = getchar();
        }
        if (type == 'q')
        {
            running = 0; // 设置标志为0，退出循环
            break;
        }
        while (getchar() != '\n')
            ; // 清空输入缓冲区中的换行符
        printf("%c 型温度范围(%f, %f), 请输入温度值(摄氏度): \n", type, get_min_temp(type), get_max_temp(type));
        int flag = scanf("%f", &t);
        if (flag != 1)
        {
            printf("输入错误，请重新输入\n");
            while (getchar() != '\n')
                ;     // 清空输入缓冲区中的错误字符
            continue; // 跳过当前循环，重新输入
        }
        // 清除输入缓冲区中可能残留的换行符（通常scanf读取成功后会留下一个换行符）
        while (getchar() != '\n')
            ;
        switch (type)
        {
        case 'k':
            res = calculate_k(t);
            break;
        case 'b':
            res = calculate_b(t);
            break;
        case 'e':
            res = calculate_e(t);
            break;
        case 'j':
            res = calculate_j(t);
            break;
        case 'n':
            res = calculate_n(t);
            break;
        case 't':
            res = calculate_t(t);
            break;
        case 'r':
            res = calculate_r(t);
            break;
        case 's':
            res = calculate_s(t);
            break;
        default:
            break;
        }
        printf("电压值为(mV): %f\n\n", res);
    }
    return 0;
}